package com.lancoo.znbkxx.teacher.aibkTestCoach.questionToEvaluation

import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.bindLifeCycle
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.viewModel.MarkViewModel
import com.lancoo.znbkxx.teacher.aibkTestCoach.questionToEvaluation.aibkQuesEvalutionDXT.QuesEvaluationFragmentDxt
import com.lancoo.znbkxx.teacher.aibkTestCoach.questionToEvaluation.aibkQuesEvalutionGCT.QuesEvaluationFragmentGct
import com.lancoo.znbkxx.teacher.aibkTestCoach.questionToEvaluation.aibkQuesEvalutionJDT.QuesEvaluationFragmentJdt
import com.lancoo.znbkxx.teacher.aibkTestCoach.questionToEvaluation.aibkQuesEvalutionPPT.QuesEvaluationFragmentPpt
import com.lancoo.znbkxx.teacher.aibkTestCoach.questionToEvaluation.aibkQuesEvalutionTKT.QuesEvaluationFragmentTkt
import com.lancoo.znbkxx.teacher.aibkTestCoach.questionToEvaluation.aibkQuesEvalutionWXXZ.QuesEvaluationFragmentWxxz
import com.lancoo.znbkxx.teacher.aibkTestCoach.questionToEvaluation.aibkQuesEvalutionXZT.QuesEvaluationFragmentXzt
import com.lancoo.znbkxx.teacher.aibkTestCoach.questionToEvaluation.aibkQuesEvalutionYDPP.QuesEvaluationFragmentYdpp
import com.lancoo.znbkxx.teacher.aibkTestCoach.questionToEvaluation.aibkQuesEvalutionZHT.QuesEvaluationFragmentZht
import com.lancoo.znbkxx.teacher.aibkTestCoach.questionToEvaluation.aibkQuesEvalutionZW.QuesEvaluationFragmentZw
import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.fragment.app.Fragment
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseActivity
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseFragmentPagerAdapter
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.QuesEvaluationDetail
import com.lancoo.znbkxx.teacher.aibkTestCoach.questionToEvaluation.aibkQuesEvalutionFhxz.QuesEvaluationFragmentFhxz
import kotlinx.android.synthetic.main.aibk_activity_single_ques_evaluation.*
import kotlinx.android.synthetic.main.aibk_include_loading_refresh.*
import org.koin.androidx.viewmodel.ext.android.viewModel

class AibkSingleQuesEvaluationActivity : KBaseActivity(), View.OnClickListener {

    companion object {
        fun launch(activity: Activity, paperId: String, quesId: String, typeNo: String,genreName:String) {
            val intent = Intent(activity, AibkSingleQuesEvaluationActivity::class.java)
            intent.putExtra("PaperID", paperId)
            intent.putExtra("QuesID", quesId)
            intent.putExtra("TypeNo", typeNo)
            intent.putExtra("GenreName", genreName)
            activity.startActivity(intent)
        }
    }

    private val mViewModel: MarkViewModel by viewModel()
    private var paperId = ""
    private var quesId = ""
    private var typeNo = ""
    private var genreName = ""
    private var mFragmentList: MutableList<Fragment>? = ArrayList()

    override fun setLayout(): Int {
        return R.layout.aibk_activity_single_ques_evaluation
    }

    override fun initView() {
        BtnBack.setOnClickListener(this)
        LoadPaperView()
    }

    override fun initData() {
        paperId = intent.getStringExtra("PaperID")
        quesId = intent.getStringExtra("QuesID")
        typeNo = intent.getStringExtra("TypeNo")
        genreName = intent.getStringExtra("GenreName")
    }

    private fun LoadPaperView() {
        //显示试卷名称以及答题卡按钮
        STvQuesName.text = genreName

        //加载试题,显示试卷作答情况
        mFragmentList?.clear()
        val startIndex = 1

        if (typeNo == "A") {
            val mFragment = QuesEvaluationFragmentDxt(this, quesId, startIndex)
            (mFragmentList as ArrayList<Fragment>).add(mFragment)
        } else if (typeNo == "C") {
            val mFragment = QuesEvaluationFragmentWxxz(this, quesId, startIndex)
            (mFragmentList as ArrayList<Fragment>).add(mFragment)
        } else if (typeNo == "L" ||typeNo == "E" || typeNo == "F" || typeNo == "D") {//听力选择，听力判断，阅读选择
            val mFragment = QuesEvaluationFragmentXzt(this, quesId, startIndex)
            (mFragmentList as ArrayList<Fragment>).add(mFragment)
        } else if (typeNo == "k") {
            val mFragment = QuesEvaluationFragmentZht(this, quesId, startIndex)
            (mFragmentList as ArrayList<Fragment>).add(mFragment)
        }else if (typeNo == "H") {
            val mFragment = QuesEvaluationFragmentFhxz(this, quesId, startIndex)
            (mFragmentList as ArrayList<Fragment>).add(mFragment)
        }  else if (typeNo == "f") {
            val mFragment = QuesEvaluationFragmentZw(this, quesId, startIndex)
            (mFragmentList as ArrayList<Fragment>).add(mFragment)
        } else if (typeNo == "N" || typeNo == "S") { //N:普通填空题  S：听力填空题
            val mFragment = QuesEvaluationFragmentTkt(this, quesId, startIndex)
            (mFragmentList as ArrayList<Fragment>).add(mFragment)
        } else if (typeNo == "Q" || typeNo == "b" || typeNo == "d"
                || typeNo == "X" || typeNo == "g" || typeNo == "V"
                || typeNo == "G") {//Q:阅读填空,X:阅读简答,G:听力简答,
            val mFragment = QuesEvaluationFragmentJdt(this, quesId, startIndex)
            (mFragmentList as ArrayList<Fragment>).add(mFragment)
        } else if (typeNo == "x" || typeNo == "m") {//x:听力匹配,m:普通匹配题
            val mFragment = QuesEvaluationFragmentPpt(
                    this, quesId, startIndex)
            (mFragmentList as ArrayList<Fragment>).add(mFragment)
        } else if (typeNo == "U") {
            val mFragment = QuesEvaluationFragmentGct(this, quesId, startIndex)
            (mFragmentList as ArrayList<Fragment>).add(mFragment)
        } else if (typeNo == "P") {
            val mFragment = QuesEvaluationFragmentYdpp(this, quesId, startIndex)
            (mFragmentList as ArrayList<Fragment>).add(mFragment)
        }

        val mAdapter = KBaseFragmentPagerAdapter(supportFragmentManager, mFragmentList as ArrayList<Fragment>)
        VpSingleQuesEvaluation.adapter = mAdapter

    }

    /*
    * 获取单个题目的试题详情
    * */
    fun getSingleQuesEvaluation(quesId: String, startIndex: Int) {

        mViewModel.getQuesEvaluationDeatil(paperId, quesId, startIndex)
                .doOnSubscribe {
                    RlGifLoading.visibility = View.VISIBLE
                }.bindLifeCycle(this)
                .subscribe({
                    RlGifLoading.visibility = View.GONE
                    detailListener!!.getQuesDetailSuccess(it.data)
                }, {
                })
    }

    interface OnDetailListener {
        fun getQuesDetailSuccess(quesEvaluationDetail: QuesEvaluationDetail)
        fun getQuesDetailFaile()
    }

    private var detailListener: OnDetailListener? = null

    fun setDetailListener(onDetailListener: OnDetailListener?) {
        this.detailListener = onDetailListener
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.BtnBack -> {
                finish()
            }
        }
    }
}